package com.archgeek.calcite.test;

import com.alibaba.druid.sql.ast.statement.SQLTableSource;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;

import java.io.Serializable;
import java.util.List;

/**
 * @author Lancerlin
 * date: 2018/11/9 10:45
 * @version 1.0
 */
public class SqlParseInfo implements Serializable {
    @Override
    public String toString() {
        return "SqlParseInfo{" +
                "selectPairs=" + selectPairs +
                ", groupByString='" + groupByString + '\'' +
                ", whereString='" + whereString + '\'' +
                ", orderbyString='" + orderbyString + '\'' +
                ", relationByString='" + relationByString + '\'' +
                ", limitString='" + limitString + '\'' +
                '}';
    }

    private List<ImmutablePair<String, String>> selectPairs;
    private String groupByString;
    private String whereString;
    private String orderbyString;
    private String relationByString;
    private SQLTableSource tableSource;
    private String hints;
    private String limitString;

    public String getHints() {
        return hints;
    }

    public void setHints(String hints) {
        this.hints = hints;
    }

    public List<ImmutablePair<String, String>> getSelectPairs() {
        return selectPairs;
    }

    public void setSelectPairs(List<ImmutablePair<String, String>> selectPairs) {
        this.selectPairs = selectPairs;
    }

    public String getGroupByString() {
        return groupByString;
    }

    public void setRelationByString(String relationByString) {
        this.relationByString = relationByString;
    }

    public void setTableSource(SQLTableSource source) {
        tableSource = source;

    }

    public SQLTableSource getTableSource() {
        return tableSource;
    }

    public String getRelationByString() {
        return relationByString;
    }

    public void setGroupByString(String groupByString) {
        this.groupByString = groupByString;
    }

    public String getWhereString() {
        return whereString;
    }

    public void setWhereString(String whereString) {
        this.whereString = whereString;
    }

    public String getOrderbyString() {
        return orderbyString;
    }

    public void setOrderbyString(String orderbyString) {
        this.orderbyString = orderbyString;
    }

  public String getLimitString() {
    return limitString;
  }

  public void setLimitString(String limitString) {
    this.limitString = limitString;
  }

  public String getGroupByClos(boolean window) {
        StringBuilder sb = new StringBuilder();
        String groupByString = this.getGroupByString();
        if (StringUtils.isBlank(groupByString)) {
            return sb.toString();
        }
        String[] split = groupByString.split(",");
        for (String s : split) {
            if (window) {
                if (s.startsWith("window(")) {
                    sb.append(s);
                    return sb.toString();
                } else {
                    sb.append(s).append(",");
                }
            }
        }
        if (sb.toString().endsWith(",")) {
            return sb.toString().substring(0, sb.toString().length());
        }
        return sb.toString();
    }
}
